package edu.hawaii.duedates.wicket;

import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.behavior.HeaderContributor;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.PropertyModel;
import edu.hawaii.duedates.BorrowedItem;
import edu.hawaii.duedates.configuration.jaxb.Library;
import edu.hawaii.duedates.configuration.jaxb.User;

/**
 * Display page for DueDates 2.0, contains the table with all items checked out.
 * 
 * @author John Ly
 *
 */
public class DisplayPage extends BasePage {
  
  /** Support serialization. */
  private static final long serialVersionUID = 1L;
  
  protected String name = "";
  
  protected int within = 360;
  protected int withinAll = 360;
  

  /**
   * Creates a Display Page which holds the results.
   */
  public DisplayPage () {
    
    add(HeaderContributor.forCss(edu.hawaii.duedates.wicket.DisplayPage.class, 
        "style.css", "screen"));
    
    final DueDatesSession session = (DueDatesSession)getSession();
    User signedIn = getDueDates().getUser();
    
    Form form = new Form("topForm");
    //try {
      if (signedIn == null) {
        setResponsePage(new Index());        
      }
      
      else {
        add(new Label("Welcome", "Welcome " + signedIn.getName()));
        
        List<String> libraryNames = new ArrayList<String>();
        List<Library> listLibrary = signedIn.getLibrary();
        for (Library lib : listLibrary) {
          libraryNames.add(lib.getName() + "........Account: " + lib.getUsername());
        }

        DataView libview = new DataView("LibraryList", new ListDataProvider(libraryNames)) {

          private static final long serialVersionUID = 1L;

          public void populateItem(Item item) {
            name = (String) item.getModelObject();
            item.add(new Button("update") {              
              /** Support serialization */
              private static final long serialVersionUID = 1L;
              private String label = name;

              @Override
              public void onSubmit() {
                session.setIsAllLib(false);
                if (label.startsWith("University of Hawaii")) {
                  getDueDates().setLibraryFilterOutHsl();
                  getDueDates().retrieveInfo();
                  setResponsePage(DisplayPage.class);
                }
                if (label.startsWith("Hawaii State Library")) {
                  getDueDates().setLibraryFilterOutUhm();
                  getDueDates().retrieveInfo();
                  setResponsePage(DisplayPage.class);
                }
              }
            });
            item.add(new Label("LibraryName", name));
            item.add(new TextField("Within", new PropertyModel(session, "within")));
          }
        };
        form.add(libview);
        form.add(new TextField("WithinAll", new PropertyModel(session, "withinAll")));
        form.add(new Button("updateAll") {
          private static final long serialVersionUID = 1L;
          
          @Override
          public void onSubmit() {
            session.setIsAllLib(true);
            getDueDates().setLibraryFilterNone();
            getDueDates().retrieveInfo();
            setResponsePage(DisplayPage.class);
          }
        });
        add(form);
        
        add(new Link("sortDateLink") {
          private static final long serialVersionUID = 1L;
          @Override
          public void onClick() {
            getDueDates().sortBy("date");
            setResponsePage(DisplayPage.class);
          }
        });
        
        add(new Link("sortLibraryLink") {
          private static final long serialVersionUID = 1L;
          @Override
          public void onClick() {
            getDueDates().sortBy("library");
            setResponsePage(DisplayPage.class);
          }
        });
        
        //getDueDates().retrieveInfo();
        
        List<BorrowedItem> listItem = getDueDates().getItemList();
        //Populate list according to the value of within
        if (session.isAllLib()) {
          listItem = getDueDates().withinNumOfDays(listItem, session.getWithinAll());
        }
        else {
          listItem = getDueDates().withinNumOfDays(listItem, session.getWithin());
        }
        

        DataView dataview = new DataView("ItemList", new ListDataProvider(listItem)) {
          
          private static final long serialVersionUID = 1L;

          public void populateItem(final Item item) {
            BorrowedItem bItem = (BorrowedItem) item.getModelObject();
            item.add(new Label("Title", bItem.getTitle()));
            item.add(new Label("DueDate", bItem.printDueDate()));
            item.add(new Label("Type", bItem.getType()));
            item.add(new Label("Lender", bItem.getLender()));

            item.add(new AttributeModifier("class", true, new AbstractReadOnlyModel() {
                private static final long serialVersionUID = 1L;
                
                public Object getObject() {
                    return (item.getIndex() % 2 == 0) ? "odd" : "even";
                }
            }));
          }
        };
        add(dataview);
      }
  }
}
